/**
 * Created by Administrator on 2017/7/13.
 */
angular.module('module.originator_list',[])
.config(['$stateProvider',function ($stateProvider) {
    $stateProvider
        .state('home.originator_list',{
            url:'/originator_list',
            templateUrl:'view/originator_list.html',
            controller:'originator_list_controller'
        })
}])
.controller('originator_list_controller',['$scope','$state','OriginatorService',
    function ($scope,$state,OriginatorService) {
        console.log("组织者控制器启动成功");
        var query_terms={
            originatorState:0,
            pageNum:1
        };
//分页模块开始，该模块需要配置get_pagesum函数以及get_list_data_by_pagenum函数,并依赖angularjs以及util.controller.js(晓峰的工具函数库)-------------------------------------------------------------------------------------------------------------------

        // 页码部分初始化开始
        var pagesum;
        $scope.pagenum_list=[];
        var current_page=1;
        $scope.pagination_show=true;

       /* //获取总页数(该函数附带回调函数，用于解决异步问题)
        function get_pagesum(sfun,efun) {
            OriginatorService.getOriginatorListSumStateAndPageNum(query_terms,function (data) {

                if(sfun()){sfun()}
            },efun)
        }*/

        // 根据页数拿到该页题目列表函数
        var set_page_data_by_pagenum=function(pagenum,sfun,efun){
            query_terms.pageNum=pagenum;
            OriginatorService.getOriginatorListByState(query_terms,function (data) {
                console.log(data);
                $scope.originator_list=data.originatorList;
                pagesum=data.pageCount;
                $scope.pagenum_list=create_pagenum_list(pagesum,pagenum);
                $scope.pagination_show = pagesum == 0 || pagesum == 1;
                if (sfun){
                    sfun()
                }
            },efun);
            console.log($scope.originator_list);
        };

    //从这里开始与页面数据无关
       /* //配置pagenum_list函数
        var configure_pagenum_list=function(pagenum){
            get_pagesum(function(){

            });
        };*/

        //刷新数据函数
        function refresh_data() {
            // configure_pagenum_list(current_page);
            set_page_data_by_pagenum(current_page);
        }
        refresh_data();


    //页码交互事件

        //页码点击函数
        $scope.page_click=function(pagenum){
            current_page=pagenum;
            refresh_data();
        };

        //向前翻页函数
        $scope.page_up=function(){
            if(current_page>1){
                current_page=current_page-1;
                refresh_data();
            }else {
                alert("已经是第一页了")
            }
        };

        //向后翻页函数
        $scope.page_down=function(){
            if(current_page<pagesum){
                current_page=current_page+1;
                refresh_data();
            }else {
                alert("已经是最后一页了")
            }
        };

// 分页模块结束------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        $scope.set_state=function(state){
            query_terms.originatorState=state;
            refresh_data();
        };
}]);